﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CSP.Dao.IObjectDaos.CSP;
using CSP.DataAccess;
using CSP.Dao.CommonDao;
using CSP.Model.CSP;

namespace CSP.Dao.ObjectDaos.CSP
{
    public class SearchServiceListDao : CSPCommonDao, ISearchServiceListDao
    {
        public SearchServiceListDao(CSPContext context, bool isTransaction = false)
            : base(context, isTransaction)
        { }

        public int GetDataById(int id)
        {
            throw new NotImplementedException();
        }

        public List<Model.CSP.SearchServiceList> GetDataByIdsDefault(List<int> ids, int pageIndex, int pageSize)
        {
            var searchServiceList = _context.SearchServiceLists.Where(s => ids.Contains(s.Id)).OrderBy(s => s.OrderId).Skip(pageIndex * pageSize).Take(pageSize);
            List<SearchServiceList> result = new List<SearchServiceList>();
            if (searchServiceList.Count() != 0)
            {
                try
                {
                    result = searchServiceList.ToList();
                }
                catch (Exception e) { result = new List<SearchServiceList>(); }
                
            }
            return result;
        }

        public List<Model.CSP.SearchServiceList> GetDataByIdsOrderByOrderCount(List<int> ids, int pageIndex, int pageSize, bool isSortASC = false)
        {
            IQueryable<SearchServiceList> searchServiceList = null;
            if (!isSortASC)
            {
                searchServiceList = _context.SearchServiceLists.Where(s => ids.Contains(s.Id)).OrderByDescending(s => s.OrderCount).Skip(pageIndex * pageSize).Take(pageSize);
            }
            else
            {
                searchServiceList = _context.SearchServiceLists.Where(s => ids.Contains(s.Id)).OrderBy(s => s.OrderCount).Skip(pageIndex * pageSize).Take(pageSize);
            }

            List<SearchServiceList> result = new List<SearchServiceList>();
            if (searchServiceList.Count() != 0)
            {
                result = searchServiceList.ToList();
            }
            return result;
        }

        public List<Model.CSP.SearchServiceList> GetDataByIdsOrderByServiceCredit(List<int> ids, int pageIndex, int pageSize, bool isSortASC = false)
        {
            IQueryable<SearchServiceList> searchServiceList = null;
            if (!isSortASC)
            {
                searchServiceList = _context.SearchServiceLists.Where(s => ids.Contains(s.Id)).OrderByDescending(s => s.ServiceCredit).Skip(pageIndex * pageSize).Take(pageSize);
            }
            else
            {
                searchServiceList = _context.SearchServiceLists.Where(s => ids.Contains(s.Id)).OrderBy(s => s.ServiceCredit).Skip(pageIndex * pageSize).Take(pageSize);
            }

            List<SearchServiceList> result = new List<SearchServiceList>();
            if (searchServiceList.Count() != 0)
            {
                result = searchServiceList.ToList();
            }
            return result;
        }

        public List<Model.CSP.SearchServiceList> GetDataByIdsOrderByPrice(List<int> ids, int pageIndex, int pageSize, bool isSortASC = false)
        {
            IQueryable<SearchServiceList> searchServiceList = null;
            if (!isSortASC)
            {
                searchServiceList = _context.SearchServiceLists.Where(s => ids.Contains(s.Id)).OrderByDescending(s => s.Price).Skip(pageIndex * pageSize).Take(pageSize);
            }
            else
            {
                searchServiceList = _context.SearchServiceLists.Where(s => ids.Contains(s.Id)).OrderBy(s => s.Price).Skip(pageIndex * pageSize).Take(pageSize);
            }

            List<SearchServiceList> result = new List<SearchServiceList>();
            if (searchServiceList.Count() != 0)
            {
                result = searchServiceList.ToList();
            }
            return result;
        }

        /// <summary>
        /// 根据要获取的数量获取随机ServiceInfoIds
        /// </summary>
        /// <param name="count"></param>
        /// <returns></returns>
        public List<int> GetRandomServiceInfoIdsByCount(int count)
        {
            IEnumerable<int> ids = null;
            ids=_context.Database.SqlQuery<int>("select top " + count + " id from SearchServiceList Order by NewID()");
            if (ids.Count()==0)
            {
                return new List<int>();
            }

            return ids.ToList();
        }
    }
}
